package com.jy.adscm.operation.aftersales.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.aftersales.bean.ShRepair;
import com.jy.adscm.operation.aftersales.bean.ShRepairUpkeep;
import com.jy.adscm.operation.aftersales.bean.ShRepairUpkeepRepairPartsMaterialList;
import com.jy.adscm.operation.aftersales.mapper.mysql.ShRepairMapper;
import com.jy.adscm.operation.aftersales.mapper.mysql.ShRepairUpkeepMapper;
import com.jy.adscm.operation.aftersales.mapper.mysql.ShRepairUpkeepRepairPartsMaterialListMapper;
import com.jy.adscm.operation.aftersales.service.ShRepairService;
import com.jy.adscm.operation.aftersales.service.ShRepairUpkeepRepairPartsMaterialListService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.operation.mscyb.bean.JqbzSettings;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqbzSettingsMapper;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-08-29
 */
@Service
public class ShRepairUpkeepRepairPartsMaterialListServiceImpl extends ServiceImpl<ShRepairUpkeepRepairPartsMaterialListMapper, ShRepairUpkeepRepairPartsMaterialList> implements ShRepairUpkeepRepairPartsMaterialListService {
    @Autowired
    JqbzSettingsMapper jqbzSettingsMapper;
    @Autowired
    JyUserMapper jyUserMapper;
    @Autowired
    ShRepairService  shRepairService;
    @Autowired
    ShRepairUpkeepMapper shRepairUpkeepMapper;
    @Autowired
    ShRepairMapper  shRepairMapper;
    @Override
    public boolean insertShRepairUpkeepRepairPartsMaterialList(ShRepairUpkeepRepairPartsMaterialList shRepairUpkeepRepairPartsMaterialList) throws AdscmSystemException {
        //添加部件修理
        Wrapper<ShRepairUpkeepRepairPartsMaterialList> wrapper=new EntityWrapper<>();
        wrapper.eq("code",shRepairUpkeepRepairPartsMaterialList.getCode());
        List<ShRepairUpkeepRepairPartsMaterialList> shRepairUpkeepRepairPartsMaterialLists = super.selectList(wrapper);
        if (shRepairUpkeepRepairPartsMaterialLists!=null&&shRepairUpkeepRepairPartsMaterialLists.size()>0){
            throw  new AdscmSystemException("条码有重复的，请认真核对");
        }
        shRepairUpkeepRepairPartsMaterialList.getCode();
        ShRepairUpkeep shRepairUpkeep = shRepairUpkeepMapper.selectById(shRepairUpkeepRepairPartsMaterialList.getPid());
        ShRepair shRepair =new ShRepair();
        shRepair.setCustomer(shRepairUpkeep.getCustomer().toString());
        shRepair.setMachine(shRepairUpkeep.getMachine().toString());
        shRepair.setPartName(shRepairUpkeepRepairPartsMaterialList.getName());
        shRepair.setPartCode(shRepairUpkeepRepairPartsMaterialList.getCode());
        shRepair.setPartType(shRepairUpkeepRepairPartsMaterialList.getType());
        shRepair.setPartBatchCode(shRepairUpkeepRepairPartsMaterialList.getBatch());
        shRepair.setCreator(shRepairUpkeepRepairPartsMaterialList.getCreator());
        shRepair.setCreateTime(new Date());
        shRepairService.insertShRepair(shRepair);
        boolean insert = super.insert(shRepairUpkeepRepairPartsMaterialList);
        return insert;
    }

    @Override
    public List<ShRepairUpkeepRepairPartsMaterialList> selectByPid(Integer pid) {
        Wrapper<ShRepairUpkeepRepairPartsMaterialList> wrapper=new EntityWrapper<>();
        wrapper.eq("pid",pid);
        List<ShRepairUpkeepRepairPartsMaterialList> shRepairUpkeepRepairPartsMaterialLists = super.selectList(wrapper);
        for (ShRepairUpkeepRepairPartsMaterialList shRepairUpkeepRepairPartsMaterialList : shRepairUpkeepRepairPartsMaterialLists) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(shRepairUpkeepRepairPartsMaterialList.getName(),null)){
                JqbzSettings jqbzSettings = jqbzSettingsMapper.selectById(shRepairUpkeepRepairPartsMaterialList.getName());
                shRepairUpkeepRepairPartsMaterialList.setNameName(jqbzSettings.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(shRepairUpkeepRepairPartsMaterialList.getCreator(),null)){
                JyUser jyUser = jyUserMapper.selectById(shRepairUpkeepRepairPartsMaterialList.getCreator());
                shRepairUpkeepRepairPartsMaterialList.setCreatorName(jyUser.getName());
            }
        }
        return shRepairUpkeepRepairPartsMaterialLists;
    }

    @Override
    public Boolean deleteShRepairUpkeepRepairPartsMaterialList(Integer id)throws AdscmSystemException {
        ShRepairUpkeepRepairPartsMaterialList shRepairUpkeepRepairPartsMaterialList = super.selectById(id);
         //删除维修库物料
        Wrapper<ShRepair> wrapper=new EntityWrapper<>();
        wrapper.eq("part_code",shRepairUpkeepRepairPartsMaterialList.getCode());
        List<ShRepair> shRepairs = shRepairMapper.selectList(wrapper);
        if (shRepairs!=null &&shRepairs.size()>0){
            ShRepair shRepair = shRepairs.get(0);
            if (shRepair.getState()!=0){
                throw  new AdscmSystemException("维修任务一开始了，不能删除了");
            }
            shRepairMapper.deleteById(shRepair.getId());
        }
        boolean b = super.deleteById(shRepairUpkeepRepairPartsMaterialList.getId());
        return b;
    }

}
